Kaba syntax ed

My general purpose programming language.

Full lib reference: kaba reference

The basics ed

A basic main() function:

func main()
    print("hi")

Parameter and return type syntax:

# compiler decides if call-by-value/reference, but always IMMUTABLE
func f(a: int, b: string) -> int
    # squaring a
    return a^2

# force call-by-reference and make i mutable
func g(out i: int)
    i = 13

Local variables

func main()
    # guess the type
    let i = 13
    let f = sin(pi)
    let s = "hallo"

    # explicit type
    let v: vec3

String interpolation

func main()
    let i = 13
    print("today, i is {{i}}!")

Dynamic arrays

func f(i: int) -> int[]
    let a = [1, 2, 3]
    print(len(a)) # 3
    print(sum(a)) # 6
    print(str(a)) # "[1, 2, 3]"

    let b: int[]
    # append one element
    b.add(i)

    # sub-range
    return a[1:]

For loops:

func main()
    let a = [1, 2, 3]
    for i in a
        print(i)

    for k=>i in a
        print("  a[{{k}}] = {{i}} ")

    # python-style set builder notation
    let aa = [for i in a  i^2]
    let bb = [for i in a  i^2  if i > 1]

Dictionaries:

func main()
    let a = {"red": 13, "blue": 2}
    let b: int{}
    print(a["red"])

Classes ed

Basics ed

Some member variables and functions:

class A
    i: int
    s: string
    x = 1.0 # auto assignment in constructor

    func f()
        print(s)

    # by default, the "self" object is immutable!
    func mut set_x(f: float)
        x = f

    # will be const/mutable when called on a const/mutable instance
    func ref get_a_reference_to_member() -> string
        # "ownership bleeding"
        return s[2:4]

    const N = 8

    class B # is in namespace A
        k: int

Instantiation on stack and heap:

func main()
    let a: A
    let b: A* = new A()
    # always "." for members!
    a.f()
    b.f()

Virtual functions ed

Only single inheritance supported.

class A
   func virtual f()
      print("A")

class B extends A
    func override f()
       print("B")

func main()
    let a: A* = new B()
    a.f() # prints "B"

Operators ed

Python style special member functions are used as operators:

class A
    i: int
    func mut __assign__(o: A)
        i = o.i
    func mut __iadd__(o: A)
        i += o.i

func main()
    let a, b: A
    a += b

Shared pointers ed

Shared pointers will count references and automatically destroy the object when the last shared pointer vanishes.

Classes need to explicitly support shared pointers!

class A as shared
    i: int

func main()
    let a: A shared = new A()
    a.i = 8

Function pointers ed

"int->int" is the type of a function pointer taking an int and returning an int:

func do_something(f: int->int) -> int
    return f(13)

func add_one(i: int) -> int
    return i+1

func main()
    do_something(add_one)

    let x = 4
    # a lambda function with auto-capture
    do_something(func(i: int) i^2 + x)

Cool stuff

func main()
    let f = [1.0, 2.0, 3.0]
    print(map(sqrt, f))

    let a = sorted([1, 5, 2, 6, 3, 4])

    let v: vec3[] = [[1,2,3], [4,5,6], [7,8,9]]
    let w = sorted(v, "-x") # sort by the x component, decreasing

Generics ed

This is still experimental!

func ultra_max[T](a: T, b: T) -> T
    if a > b
        return a
    return b

func main()
    print(ultra_max(1, 4))
    print(ultra_max(1.0, 4.0))
    let f = ultra_max[int]

Categories: Programmieren